/*================================================================*
        Copyright (C) 2021 All rights reserved, www.hsby.link.
      	文件名称：socket.cc
      	创 建 者：汉森伯逸
      	创建日期：2022/01/01
 *================================================================*/

#include "http/http_server.h"

void Run() {
    auto server = hsby::http::HttpServer::Create("TestHttpSrv", true);
    if (!server->Init()) {
        //HSBY_LOG_SYSTEM_DEBUG << "server->Init() fail";
        return;
    }
    if (!server->Bind(hsby::Ipv4Address::Create("0.0.0.0", 80))) {
        //HSBY_LOG_SYSTEM_DEBUG << "server->Bind(hsby::Ipv4Address::Create(\"0.0.0.0\", 10086)) fail";
        return;
    }
//    auto ssd = server->GetServletDispatch();
//    ssd->SetServlet("/", [](hsby::http::HttpRequest::Ptr req,
//                                    hsby::http::HttpResponse::Ptr rsp,
//                                    hsby::http::HttpSession::Ptr session) -> int {
//        rsp->SetBody("Welcome to hsby station root your reqs is : \n" + req->ToString());
//        return 0;
//    });
//    ssd->SetServlet("/hsby", [](hsby::http::HttpRequest::Ptr req,
//                                        hsby::http::HttpResponse::Ptr rsp,
//                                        hsby::http::HttpSession::Ptr session) -> int {
//        rsp->SetBody("Welcome to hsby station /hsby");
//        return 0;
//    });
//    ssd->SetGlobServlet("/hsby/*", [](hsby::http::HttpRequest::Ptr req,
//                                              hsby::http::HttpResponse::Ptr rsp,
//                                              hsby::http::HttpSession::Ptr session) -> int {
//        rsp->SetBody("/hsby/*");
//        return 0;
//    });
//    ssd->SetGlobServlet("/hsbyx/*", [](hsby::http::HttpRequest::Ptr req,
//                                              hsby::http::HttpResponse::Ptr rsp,
//                                              hsby::http::HttpSession::Ptr session) -> int {
//        rsp->SetBody("/hsbyx/*");
//        return 0;
//    });
    server->Start();
}

int main(int argc, char** args) {
    if (argc < 2) {
        HSBY_LOG_SYSTEM_ERROR << "Usage: bin ThreadCount";
        return -1;
    }
    HSBY_LOG_NAME("system")->setLevel(hsby::LogLevel::ERROR);
    int thread_count = atoi(args[1]);
    //HSBY_LOG_SYSTEM_DEBUG << HSBY_VARS_EXP(thread_count);
    hsby::IoManager iom(thread_count, "HttpWorkIom");
    iom.AddTask(Run);
    return 0;
}